package com.nhncorp.pleiades.demo;

import java.io.IOException;

import com.nhncorp.pleiades.client.LockService;
import com.nhncorp.pleiades.client.LockServiceClient;
import com.nhncorp.pleiades.client.PleiadesUser;
import com.nhncorp.pleiades.client.LockService.LockMode;
import com.nhncorp.pleiades.conf.PleiadesConfiguration;

public class FailOver {

  private String globalPath = "/com/nhncorp/nebula/failover";
  private PleiadesConfiguration conf;
  private long maxNumber = 100000;
  
  public FailOver() {
    try {
      globalPath += String.valueOf(System.currentTimeMillis());
      conf = new PleiadesConfiguration();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  public class Server implements Runnable {
  
    private LockService pleiades = null;
    private boolean stopServer = false;
    private String description = "";
    private String ownerId = "";
    
    public Server (String description, String ownerId) {
      this.description = description;
      this.ownerId = ownerId;
    }
    
    public void run() {
      System.out.println(this.description);
      try {
        pleiades = new LockServiceClient(new PleiadesUser("default", ownerId), conf);
        pleiades.getLock(globalPath, LockMode.LOCK_EXCLUSIVE);
        doLoop();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    private void doLoop() {
      long sum = 0;
      while(!stopServer) {
        try {
          if(pleiades.getLock(globalPath, LockMode.LOCK_EXCLUSIVE)) {
            String sumValue = pleiades.getContents(globalPath);
            if(!sumValue.equals("")) {
              if(sumValue.equals("0")) {
                sumValue = "1";
              }
              sum += Long.parseLong(sumValue);
            }
            System.out.println(this.description + " value of sum: "  + String.valueOf(sum));
            pleiades.setContents(globalPath, String.valueOf(sum));
            if(sum > maxNumber) {
              stopServer = true;
              sum = 0;
            }
          }
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
          return;
        } catch (NumberFormatException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      pleiades.close();  
    }
    
  }
  
  private void serverJob(int testOption) throws InterruptedException {
    if( testOption == 1) {
      new Thread(new Server("Primary", "primary")).start();
    } else if (testOption == 2){
      new Thread(new Server("Secondary", "secondary")).start();
    } else if( testOption == 3) {
      new Thread(new Server("Primary", "primary")).start();
      Thread.sleep(1000);
      new Thread(new Server("Secondary", "secondary")).start();
    }
    
  }
  
  public static void main(String[] args) throws InterruptedException {
    FailOver failOver= new FailOver();
    if(args.length > 0) {
      failOver.serverJob(Integer.parseInt(args[0]));      
    } else {
      failOver.serverJob(3);  
    }
    
  }
}
